package com.team.app.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.team.app.model.Employee;

@Repository
public class EmployeeDaoImpl extends AbstractDao<Employee> implements EmployeeDao {
	public EmployeeDaoImpl(){
		super(Employee.class);	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Employee> findAll(Long companyId) {
		Session s=this.getCurrentSession();
		if(companyId != null){
			Query q = s.createQuery("from Employee o where  o.companyId.companyId = ?");
			q.setParameter(0, companyId);		
			List<Employee> result = q.list();		
			return result;	
		}else{
			Query q = s.createQuery("from Employee o where  o.companyId.companyId is null");				
			List<Employee> result = q.list();		
			return result;	
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Employee> findAll(Employee obj, Long companyId) {
		String hql = "from Employee o where  o.companyId.companyId = "+companyId;;
		if(companyId == null){
			hql = "from Employee o where  o.companyId is null";
		}
		
		if(obj.getEmpName() != null){
			hql += " and o.empName like '%"+obj.getEmpName()+"%'";
		}		
		if(obj.getEmpMail() != null){
			hql += " and o.empMail like '%"+obj.getEmpMail()+"%'";
		}
		if(obj.getEmpPhone() != null){
			hql += " and o.empPhone like '%"+obj.getEmpPhone()+"%'";
		}
		
		Session s=this.getCurrentSession();
		Query q = s.createQuery(hql);	
		List<Employee> result = q.list();		
		return result;	
	}

	@Override
	@SuppressWarnings("unchecked")
	public boolean existEmail(String email) {
		// TODO Auto-generated method stub
		Session s=this.getCurrentSession();		
			Query q = s.createQuery("from Employee o where  o.empMail = ?");
			q.setParameter(0, email);		
			List<Employee> result = q.list();	
			if(result.size() > 0){
				return true;
			}			
			return false;		
	}
}
